package com.jy.api.domain;

import com.jy.api.enums.OrderItemMakeType;
import com.jy.api.util.UuidUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hanxi on 2017/7/17.
 */
@Entity
@Data
@NoArgsConstructor
public class OrderItem implements Serializable {
    @Id
    @Column(length = 50)
    private String id;
    @Version
    protected int version = 0;
    private String productName;
    private String productId;
    private String productImageURL;
    private String productTypeId;
    private double salesPrice = 0;

    private double dealPrice = 0;

    private double costPrice = 0;
    private int quantity = 0;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    private boolean enabled = true;



    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "addTime")
    @JoinColumn(name = "order_item_id")
    private List<OrderItemExtra> items = new ArrayList<>();

    private String mobileTag;
    /**
     * 固定提成金额
     */
    private double commission = 0;
    /**
     * 是否支持厨房打印
     */
    private boolean kitchenPrintEnabled = false;

    /**
     * 是否支持热敏打印
     */
    private boolean isDirectTherma = false;

    private boolean labelEnabled=false;


    private int ipId;

    private String ipName="";

    private int port=0;

    private String ipAddress="";
    @ApiModelProperty("是否配品订单详情")
    private boolean matchEnabled=false;

    @ApiModelProperty("是否含有配品商品")
    private boolean haveMatchEnabled=false;
    @ApiModelProperty("主订单记录id")
    private String masterItemId;

    //是否开启配品点单
    private  boolean openMatchEnabled=false;

    @Type(type = "com.jy.api.domain.json.StringArrayJson")
    @Column(columnDefinition = "text")
    private List<String> specs=new ArrayList<>();

    public OrderItem(String id, Product product,
                     double salesPrice, double dealPrice, int quantity,
                     double commission) {

        this.id = id;
        this.productName = product.getName();
        this.productId = product.getId();
        this.salesPrice = salesPrice;
        this.dealPrice = dealPrice;
        this.quantity = quantity;
        this.commission = commission;
        this.productImageURL = product.getImageURL();
        this.productTypeId = product.getProductType().getId();
        this.isDirectTherma = product.isDirectThermal();
        this.labelEnabled=product.isLabelEnabled();
    }

    //测试修改订单详细
    public OrderItem(String id, Product product, int quantity,
                     double commission) {

        this.id = id;
        this.productName = product.getName();
        this.productId = product.getId();
        this.quantity = quantity;
        this.commission = commission;
        this.productImageURL = product.getImageURL();
        this.productTypeId = product.getProductType().getId();
        this.isDirectTherma = product.isDirectThermal();
        this.labelEnabled=product.isLabelEnabled();
    }



    public OrderItem(String id, String productId, String productName,
                     double salesPrice, double dealPrice, int quantity,
                     double commission, String productImageURL) {
        this.id = id;
        this.productName = productName;
        this.productId = productId;
        this.salesPrice = salesPrice;
        this.dealPrice = dealPrice;
        this.quantity = quantity;
        this.commission = commission;
        this.productImageURL = productImageURL;
    }
    public OrderItem(String id, String productId, String productName,
                     double salesPrice, double dealPrice, int quantity,String productTypeId) {
        this.id = id;
        this.productName = productName;
        this.productId = productId;
        this.salesPrice = salesPrice;
        this.dealPrice = dealPrice;
        this.quantity = quantity;
        this.productTypeId=productTypeId;
    }

    public void accuracy() {
        salesPrice = Math.rint(salesPrice * 100) / 100d;
        dealPrice = Math.rint(dealPrice * 100) / 100d;
        commission = Math.rint(commission * 100) / 100d;
    }

    public OrderItem(OrderItem item) {
        this.id = UuidUtil.getUuid();
        this.productName = item.getProductName();
        this.productId = item.getProductId();
        this.productTypeId = item.getProductTypeId();
        this.dealPrice = item.getDealPrice();
        this.quantity = item.getQuantity();
        this.addTime = item.getAddTime();
        this.updateTime = item.getUpdateTime();
        this.enabled = item.isEnabled();
        this.kitchenPrintEnabled = item.isKitchenPrintEnabled();
        this.commission=item.getCommission();
        this.salesPrice=item.getSalesPrice();
        this.costPrice=item.getCostPrice();
        this.productImageURL=item.getProductImageURL();
        this.labelEnabled=item.isLabelEnabled();
    }






}
